#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_uart.h"
#include "iot_errno.h"
#include "iot_dido.h"

#define TASK_STACK_SIZE     4096
#define TASK_PRIORITY       20
#define IOT_UART_ID1        1
#define IOT_UART_ID2        2
#define BUFF_SIZE           100
#define DELAY_15s           (15*1000)

static const unsigned char *uart1_data = "uart1 message from esp32";
static const unsigned char *uart2_data = "uart2 message from esp32";

extern int32_t ethernet_basic(void);
void TestOneTask(void)
{

    uint8_t val = 0;
    uint8_t val1 = 0;
    uint8_t val2 = 0;
    uint8_t val3 = 0;
    uint8_t val4 = 0;
    uint8_t val5 = 0;
    uint8_t val6 = 0;
    int32_t ret;

    IotUartAttribute uart_attr = {
        .baudRate = 115200,
        .dataBits = 8,
        .stopBits = 1,
        .parity = 0,
    };

    DigiInit();
    setDigiOutput(PIN_NO_1, DIDO_NO);
    setDigiOutput(PIN_NO_2, DIDO_NO);
    setDigiOutput(PIN_NO_3, DIDO_NO);
    setDigiOutput(PIN_NO_4, DIDO_NO);
    setDigiOutput(PIN_NO_5, DIDO_NO);
    setDigiOutput(PIN_NO_6, DIDO_NO);
    printf("DO connect COM and NO finish!\n");
    osDelay(1000);

    ret = ethernet_basic();
    if(ret != 0){
        printf("eth connect fail\r\n");
    }
    printf("eth connect success\r\n");
    osDelay(DELAY_15s);

    if(uart_is_driver_installed(IOT_UART_ID1))
    {
        printf("<<<<<<<<<<<uart has been installed_%d>>>>>>>>>>>\n" , IOT_UART_ID1);
        if(uart_driver_delete(IOT_UART_ID1) != IOT_SUCCESS)
        {
            printf("UART INIT FAILED : uart has been installed, and deinit failed\n");
        }
    }
    ret = IoTUartInit(IOT_UART_ID1, &uart_attr);
    if (ret != IOT_SUCCESS) {
        printf("Init uart1 failed ret: %d\n", ret);
    } else {
        printf("Init uart1 success!\n");
    }

    if(uart_is_driver_installed(IOT_UART_ID2))
    {
        printf("<<<<<<<<<<<uart has been installed_%d>>>>>>>>>>>\n" , IOT_UART_ID2);
        if(uart_driver_delete(IOT_UART_ID2) != IOT_SUCCESS)
        {
            printf("UART INIT FAILED : uart has been installed, and deinit failed\n");
        }
    }
    ret = IoTUartInit(IOT_UART_ID2, &uart_attr);
    if (ret != IOT_SUCCESS) {
        printf("Init uart2 failed ret: %d\n", ret);
    } else {
        printf("Init uart2 success!\n");
    }
    ret = IoTUartWrite(IOT_UART_ID1, uart1_data, strlen(uart1_data));
    if (ret == IOT_FAILURE) {
        printf("Write uart1 failed ret: %d\n", ret);
    }
    ret = IoTUartWrite(IOT_UART_ID2, uart2_data, strlen(uart2_data));
    if (ret == IOT_FAILURE) {
        printf("Write uart2 failed ret: %d\n", ret);
    }

    int i = 30;
    while (i--)
    {
        getAllDigiInput(&val);
        getDigiInput(PIN_NO_1, &val1);
        getDigiInput(PIN_NO_2, &val2);
        getDigiInput(PIN_NO_3, &val3);
        getDigiInput(PIN_NO_4, &val4);
        getDigiInput(PIN_NO_5, &val5);
        getDigiInput(PIN_NO_6, &val6);
        printf("DI1 = %d DI2 = %d DI3 = %d DI4 = %d DI5 = %d DI6 = %d\n", val1, val2, val3, val4, val5, val6);

        uint8_t uart1_buff[BUFF_SIZE] = {0};
        uint8_t uart2_buff[BUFF_SIZE] = {0};

        ret = IoTUartRead(IOT_UART_ID1, &uart1_buff, BUFF_SIZE);
        if (ret == IOT_FAILURE) {
            printf("Read uart1 failed ret: %d\n", ret);
        } else if (strlen(&uart1_buff) > 0) {
            ret = IoTUartWrite(IOT_UART_ID1, &uart1_buff, strlen(&uart1_buff));
            if (ret == IOT_FAILURE) {
                printf("Write uart1 failed ret: %d\n", ret);
            }
        }
        ret = IoTUartRead(IOT_UART_ID2, &uart2_buff, BUFF_SIZE);
        if (ret == IOT_FAILURE) {
            printf("Read uart2 failed ret: %d\n", ret);
        } else if (strlen(&uart2_buff) > 0) {
            ret = IoTUartWrite(IOT_UART_ID2, &uart2_buff, strlen(&uart2_buff));
            if (ret == IOT_FAILURE) {
                printf("Write uart2 failed ret: %d\n", ret);
            }
        }
        osDelay(2000);
    }
    ret = IoTUartSetFlowCtrl(IOT_UART_ID1, IOT_FLOW_CTRL_NONE);
    if (ret != IOT_SUCCESS) {
        printf("SetFlowCtrl uart1 failed ret: %d\n", ret);
    } else {
        printf("SetFlowCtrl uart1 success!\n");
    }
    ret = IoTUartSetFlowCtrl(IOT_UART_ID2, IOT_FLOW_CTRL_NONE);
    if (ret != IOT_SUCCESS) {
        printf("SetFlowCtrl uart2 failed ret: %d\n", ret);
    } else {
        printf("SetFlowCtrl uart2 success!\n");
    }
        ret = IoTUartDeinit(IOT_UART_ID1);
    if (ret != IOT_SUCCESS) {
        printf("Deinit uart1 failed ret: %d\n", ret);
    } else {
        printf("Deinit uart1 success!\n");
    }
    ret = IoTUartDeinit(IOT_UART_ID2);
    if (ret != IOT_SUCCESS) {
        printf("Deinit uart2 failed ret: %d\n", ret);
    } else {
        printf("Deinit uart2 success!\n");
    }
    osThreadExit();
}

void TestOneDemo(void)
{
    osThreadAttr_t attr;

    attr.name = "TEST_ONE_TASK";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = TASK_STACK_SIZE;
    attr.priority = TASK_PRIORITY;

    if (osThreadNew((osThreadFunc_t)TestOneTask, NULL, &attr) == NULL) {
        printf("Create thread failed!\n");
    } else {
        printf("Create thread success!\n");
    }
}

APP_FEATURE_INIT(TestOneDemo);